package com.hellogood.yaohuo.client.bussiness.peisong;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.bussiness.AbstractTab;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.services.PeisongService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.form.fields.FormItem;

public class PeisongTab extends AbstractTab {

	@Override
	public void initSearch() {
		searchTab = new PeisongSearchTab();
		searchTab.getSearchController().getListGrid().addDoubleClickHandler(new RecordDoubleClickHandler());
	}

	@Override
	public void initForm() {
		formTab = new PeisongFormTab(this);
	}

	@Override
	protected void initPrintBtn() {
		printBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (tabSet.getSelectedTab() == searchTab) {
					Canvas.showPrintPreview(searchTab.getSearchController().getCanvas());
				} else if (tabSet.getSelectedTab() == formTab) {
					if (printBtn.getTitle().equals(Constants.BUTTON_PRINT_MODE)) {
						formTab.getFormController().getTitleLabel().setVisible(false);
						formTab.getFormController().getForm().setVisible(false);
						formTab.getFormController().getListGrid().setVisible(false);
						formTab.getFormController().getPrintView().setVisible(true);
						printBtn.setTitle(Constants.BUTTON_PRINT);
					} else if (printBtn.getTitle().equals(Constants.BUTTON_PRINT)) {
						FormItem[] items = formTab.getFormController().getForm().getFields();
						if (items != null) {
							for (FormItem item : items) {
								item.setCanEdit(true);
							}
						}

						if (formTab.getFormController().getPrintView().isVisible()) {
							Canvas.showPrintPreview(formTab.getFormController().getPrintView());
							// Canvas.showPrintPreview(formTab.getFormController().getCanvas());
						}
						if (items != null) {
							for (FormItem item : items) {
								item.setCanEdit(false);
							}
						}
						printBtn.setTitle(Constants.BUTTON_PRINT_MODE);
						formTab.getFormController().getTitleLabel().setVisible(true);
						formTab.getFormController().getForm().setVisible(true);
						formTab.getFormController().getListGrid().setVisible(true);
						formTab.getFormController().getPrintView().setVisible(false);
					}
				}
			}
		});
	}

	private class RecordDoubleClickHandler implements DoubleClickHandler {

		@Override
		public void onDoubleClick(DoubleClickEvent event) {
			showFormDetail();
		}
	}

	private class PeisongAsyncCallback implements AsyncCallback<Form> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(Form result) {
			if (result != null) {
				formTab.getFormController().updateForm(result);
				tabSet.selectTab(formTab);
				if (result.getRzDate() != null && !result.getRzDate().equals("")) {
					showRzSuccessful();
				} else {
					showSaveSuccessful();
				}
			}
		}
	}

	@Override
	public void saveForm() {
		((PeisongFormController) formTab.getFormController()).saveForm();
	}

	@Override
	public void showFormDetail() {
		Record record = searchListGrid.getSelectedRecord();
		if (record != null) {
			String billNo = record.getAttribute("code");
			if (billNo != null) {
				PeisongService.Util.getInstance().getFormDetail(billNo, new PeisongAsyncCallback());
			}
		}
	}
}
